/*
 * 文 件 名:  ReviewsAdapter.java
 * 版    权:  Weipu Technologies Co., Ltd. Copyright 2012-2014,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  sjx206
 * 修改时间:  2015-1-8
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.epro.dx.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.RatingBar;
import android.widget.TextView;

import com.epro.dx.R;
import com.epro.dx.activity.ReviewsDetailActivity;
import com.weipu.common.facade.model.ReviewlistModel;

/**
 * 用户评论adapter
 * @author  sjx206
 * @version  [版本号, 2015-1-8]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class ReviewsAdapter extends CommonAdapter<ReviewlistModel>
{
    private RotateAnimation rotate = null;
    
    private Context context;
    
    public ReviewsAdapter(Context context, List<ReviewlistModel> commentList)
    {
        super(commentList, context);
        this.context = context;
        rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.setDuration(1080);
        rotate.setStartTime(Animation.START_ON_FIRST_FRAME);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);
    }
    
    @Override
    public View getView(final int position, View convertView, final List<ReviewlistModel> commentList, Context context)
    {
        final CommentViewHolder commentViewHolder;
        if (null == convertView)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.detail_reviews_item, null);
            commentViewHolder = new CommentViewHolder();
            commentViewHolder.reviewName = (TextView)convertView.findViewById(R.id.review_name);
            commentViewHolder.reviewCustomer = (TextView)convertView.findViewById(R.id.review_customer);
            commentViewHolder.reviewDescription = (TextView)convertView.findViewById(R.id.review_description);
            commentViewHolder.priceRating = (RatingBar)convertView.findViewById(R.id.review_rating);
            convertView.setTag(commentViewHolder);
        }
        else
        {
            commentViewHolder = (CommentViewHolder)convertView.getTag();
        }
        
        ReviewlistModel comment = (ReviewlistModel)getItem(position);
        if (null != comment)
        {
            commentViewHolder.reviewName.setText(comment.getSubject());
            commentViewHolder.reviewCustomer.setText(context.getString(R.string.detail_reviews_time, new Object[] {
                comment.getUsername(), comment.getTimestamp()}));
            commentViewHolder.reviewDescription.setText(comment.getContent());
            // 商品价格评价
            if (comment.getPriceRating() > 0)
            {
                commentViewHolder.priceRating.setRating(comment.getPriceRating());
            }
            else
            {
                commentViewHolder.priceRating.setVisibility(View.GONE);
            }
        }
        convertView.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
            	ReviewlistModel reviewModel=commentList.get(position);
            	if(null==reviewModel){
            		return;
            	}
                detailIntent(reviewModel);
            }
        });
        return convertView;
    }
    
    static class CommentViewHolder
    {
        private TextView reviewName;
        
        private TextView reviewCustomer;
        
        private TextView reviewDescription;
        
        private RatingBar priceRating;
    }
    
    /**
     * 跳转详情页面
     * 
     * @param product
     *            产品ID
     */
    protected void detailIntent(ReviewlistModel reviewModel)
    {
        //单击后进入详情
        Intent in = new Intent(context, ReviewsDetailActivity.class);
        Bundle b = new Bundle();
        b.putSerializable("reviewModel", reviewModel);
        in.putExtras(b);
        context.startActivity(in);
    }
}
